home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / perldata.z / perldata
Text File  |  1998-10-30  |  31KB  |  727 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perldata - Perl data types
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      VVVVaaaarrrriiiiaaaabbbblllleeee nnnnaaaammmmeeeessss
  13.  
  14.      Perl has three data structures: scalars, arrays of scalars, and
  15.      associative arrays of scalars, known as "hashes".  Normal arrays are
  16.      indexed by number, starting with 0.  (Negative subscripts count from the
  17.      end.)  Hash arrays are indexed by string.
  18.  
  19.      Values are usually referred to by name (or through a named reference).
  20.      The first character of the name tells you to what sort of data structure
  21.      it refers.  The rest of the name tells you the particular value to which
  22.      it refers.  Most often, it consists of a single _i_d_e_n_t_i_f_i_e_r, that is, a
  23.      string beginning with a letter or underscore, and containing letters,
  24.      underscores, and digits.  In some cases, it may be a chain of
  25.      identifiers, separated by :: (or by ', but that's deprecated); all but
  26.      the last are interpreted as names of packages, to locate the namespace in
  27.      which to look up the final identifier (see the Packages entry in the
  28.      _p_e_r_l_m_o_d manpage for details).  It's possible to substitute for a simple
  29.      identifier an expression which produces a reference to the value at
  30.      runtime; this is described in more detail below, and in the _p_e_r_l_r_e_f
  31.      manpage.
  32.  
  33.      There are also special variables whose names don't follow these rules, so
  34.      that they don't accidentally collide with one of your normal variables.
  35.      Strings which match parenthesized parts of a regular expression are saved
  36.      under names containing only digits after the $ (see the _p_e_r_l_o_p manpage
  37.      and the _p_e_r_l_r_e manpage).  In addition, several special variables which
  38.      provide windows into the inner working of Perl have names containing
  39.      punctuation characters (see the _p_e_r_l_v_a_r manpage).
  40.  
  41.      Scalar values are always named with '$', even when referring to a scalar
  42.      that is part of an array.  It works like the English word "the".  Thus we
  43.      have:
  44.  
  45.          $days               # the simple scalar value "days"
  46.          $days[28]           # the 29th element of array @days
  47.          $days{'Feb'}        # the 'Feb' value from hash %days
  48.          $#days              # the last index of array @days
  49.  
  50.      but entire arrays or array slices are denoted by '@', which works much
  51.      like the word "these" or "those":
  52.  
  53.          @days               # ($days[0], $days[1],... $days[n])
  54.          @days[3,4,5]        # same as @days[3..5]
  55.          @days{'a','c'}      # same as ($days{'a'},$days{'c'})
  56.  
  57.      and entire hashes are denoted by '%':
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  71.  
  72.  
  73.  
  74.          %days               # (key1, val1, key2, val2 ...)
  75.  
  76.      In addition, subroutines are named with an initial '&', though this is
  77.      optional when it's otherwise unambiguous (just as "do" is often redundant
  78.      in English).  Symbol table entries can be named with an initial '*', but
  79.      you don't really care about that yet.
  80.  
  81.      Every variable type has its own namespace.  You can, without fear of
  82.      conflict, use the same name for a scalar variable, an array, or a hash
  83.      (or, for that matter, a filehandle, a subroutine name, or a label).  This
  84.      means that $foo and @foo are two different variables.  It also means that
  85.      $foo[1] is a part of @foo, not a part of $foo.  This may seem a bit
  86.      weird, but that's okay, because it is weird.
  87.  
  88.      Because variable and array references always start with '$', '@', or '%',
  89.      the "reserved" words aren't in fact reserved with respect to variable
  90.      names.  (They ARE reserved with respect to labels and filehandles,
  91.      however, which don't have an initial special character.  You can't have a
  92.      filehandle named "log", for instance.  Hint: you could say
  93.      open(LOG,'logfile') rather than open(log,'logfile').  Using uppercase
  94.      filehandles also improves readability and protects you from conflict with
  95.      future reserved words.)  Case _I_S significant--"FOO", "Foo", and "foo" are
  96.      all different names.  Names that start with a letter or underscore may
  97.      also contain digits and underscores.
  98.  
  99.      It is possible to replace such an alphanumeric name with an expression
  100.      that returns a reference to an object of that type.  For a description of
  101.      this, see the _p_e_r_l_r_e_f manpage.
  102.  
  103.      Names that start with a digit may contain only more digits.  Names which
  104.      do not start with a letter, underscore,  or digit are limited to one
  105.      character, e.g.,  $% or $$.  (Most of these one character names have a
  106.      predefined significance to Perl.  For instance, $$ is the current process
  107.      id.)
  108.  
  109.      CCCCoooonnnntttteeeexxxxtttt
  110.  
  111.      The interpretation of operations and values in Perl sometimes depends on
  112.      the requirements of the context around the operation or value.  There are
  113.      two major contexts: scalar and list.  Certain operations return list
  114.      values in contexts wanting a list, and scalar values otherwise.  (If this
  115.      is true of an operation it will be mentioned in the documentation for
  116.      that operation.)  In other words, Perl overloads certain operations based
  117.      on whether the expected return value is singular or plural.  (Some words
  118.      in English work this way, like "fish" and "sheep".)
  119.  
  120.      In a reciprocal fashion, an operation provides either a scalar or a list
  121.      context to each of its arguments.  For example, if you say
  122.  
  123.          int( <STDIN> )
  124.  
  125.      the integer operation provides a scalar context for the <STDIN> operator,
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  137.  
  138.  
  139.  
  140.      which responds by reading one line from STDIN and passing it back to the
  141.      integer operation, which will then find the integer value of that line
  142.      and return that.  If, on the other hand, you say
  143.  
  144.          sort( <STDIN> )
  145.  
  146.      then the sort operation provides a list context for <STDIN>, which will
  147.      proceed to read every line available up to the end of file, and pass that
  148.      list of lines back to the sort routine, which will then sort those lines
  149.      and return them as a list to whatever the context of the sort was.
  150.  
  151.      Assignment is a little bit special in that it uses its left argument to
  152.      determine the context for the right argument.  Assignment to a scalar
  153.      evaluates the righthand side in a scalar context, while assignment to an
  154.      array or array slice evaluates the righthand side in a list context.
  155.      Assignment to a list also evaluates the righthand side in a list context.
  156.  
  157.      User defined subroutines may choose to care whether they are being called
  158.      in a scalar or list context, but most subroutines do not need to care,
  159.      because scalars are automatically interpolated into lists.  See the
  160.      wantarray entry in the _p_e_r_l_f_u_n_c manpage.
  161.  
  162.      SSSSccccaaaallllaaaarrrr vvvvaaaalllluuuueeeessss
  163.  
  164.      All data in Perl is a scalar or an array of scalars or a hash of scalars.
  165.      Scalar variables may contain various kinds of singular data, such as
  166.      numbers, strings, and references.  In general, conversion from one form
  167.      to another is transparent.  (A scalar may not contain multiple values,
  168.      but may contain a reference to an array or hash containing multiple
  169.      values.)  Because of the automatic conversion of scalars, operations, and
  170.      functions that return scalars don't need to care (and, in fact, can't
  171.      care) whether the context is looking for a string or a number.
  172.  
  173.      Scalars aren't necessarily one thing or another.  There's no place to
  174.      declare a scalar variable to be of type "string", or of type "number", or
  175.      type "filehandle", or anything else.  Perl is a contextually polymorphic
  176.      language whose scalars can be strings, numbers, or references (which
  177.      includes objects).  While strings and numbers are considered pretty much
  178.      the same thing for nearly all purposes, references are strongly-typed
  179.      uncastable pointers with builtin reference-counting and destructor
  180.      invocation.
  181.  
  182.      A scalar value is interpreted as TRUE in the Boolean sense if it is not
  183.      the null string or the number 0 (or its string equivalent, "0").  The
  184.      Boolean context is just a special kind of scalar context.
  185.  
  186.      There are actually two varieties of null scalars: defined and undefined.
  187.      Undefined null scalars are returned when there is no real value for
  188.      something, such as when there was an error, or at end of file, or when
  189.      you refer to an uninitialized variable or element of an array.  An
  190.      undefined null scalar may become defined the first time you use it as if
  191.      it were defined, but prior to that you can use the _d_e_f_i_n_e_d() operator to
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  203.  
  204.  
  205.  
  206.      determine whether the value is defined or not.
  207.  
  208.      To find out whether a given string is a valid nonzero number, it's
  209.      usually enough to test it against both numeric 0 and also lexical "0"
  210.      (although this will cause ----wwww noises).  That's because strings that aren't
  211.      numbers count as 0, just as they do in aaaawwwwkkkk:
  212.  
  213.          if ($str == 0 && $str ne "0")  {
  214.              warn "That doesn't look like a number";
  215.          }
  216.  
  217.      That's usually preferable because otherwise you won't treat IEEE
  218.      notations like NaN or Infinity properly.  At other times you might prefer
  219.      to use a regular expression to check whether data is numeric.  See the
  220.      _p_e_r_l_r_e manpage for details on regular expressions.
  221.  
  222.          warn "has nondigits"        if     /\D/;
  223.          warn "not a whole number"   unless /^\d+$/;
  224.          warn "not an integer"       unless /^[+-]?\d+$/
  225.          warn "not a decimal number" unless /^[+-]?\d+\.?\d*$/
  226.          warn "not a C float"
  227.              unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
  228.  
  229.      The length of an array is a scalar value.  You may find the length of
  230.      array @days by evaluating $#days, as in ccccsssshhhh.  (Actually, it's not the
  231.      length of the array, it's the subscript of the last element, because
  232.      there is (ordinarily) a 0th element.)  Assigning to $#days changes the
  233.      length of the array.  Shortening an array by this method destroys
  234.      intervening values.  Lengthening an array that was previously shortened
  235.      _N_O _L_O_N_G_E_R recovers the values that were in those elements.  (It used to
  236.      in Perl 4, but we had to break this to make sure destructors were called
  237.      when expected.)  You can also gain some measure of efficiency by pre-
  238.      extending an array that is going to get big.  (You can also extend an
  239.      array by assigning to an element that is off the end of the array.)  You
  240.      can truncate an array down to nothing by assigning the null list () to
  241.      it.  The following are equivalent:
  242.  
  243.          @whatever = ();
  244.          $#whatever = -1;
  245.  
  246.      If you evaluate a named array in a scalar context, it returns the length
  247.      of the array.  (Note that this is not true of lists, which return the
  248.      last value, like the C comma operator.)  The following is always true:
  249.  
  250.          scalar(@whatever) == $#whatever - $[ + 1;
  251.  
  252.      Version 5 of Perl changed the semantics of $[: files that don't set the
  253.      value of $[ no longer need to worry about whether another file changed
  254.      its value.  (In other words, use of $[ is deprecated.)  So in general you
  255.      can assume that
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  269.  
  270.  
  271.  
  272.          scalar(@whatever) == $#whatever + 1;
  273.  
  274.      Some programmers choose to use an explicit conversion so nothing's left
  275.      to doubt:
  276.  
  277.          $element_count = scalar(@whatever);
  278.  
  279.      If you evaluate a hash in a scalar context, it returns a value which is
  280.      true if and only if the hash contains any key/value pairs.  (If there are
  281.      any key/value pairs, the value returned is a string consisting of the
  282.      number of used buckets and the number of allocated buckets, separated by
  283.      a slash.  This is pretty much useful only to find out whether Perl's
  284.      (compiled in) hashing algorithm is performing poorly on your data set.
  285.      For example, you stick 10,000 things in a hash, but evaluating %HASH in
  286.      scalar context reveals "1/16", which means only one out of sixteen
  287.      buckets has been touched, and presumably contains all 10,000 of your
  288.      items.  This isn't supposed to happen.)
  289.  
  290.      SSSSccccaaaallllaaaarrrr vvvvaaaalllluuuueeee ccccoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  291.  
  292.      Numeric literals are specified in any of the customary floating point or
  293.      integer formats:
  294.  
  295.          12345
  296.          12345.67
  297.          .23E-10
  298.          0xffff              # hex
  299.          0377                # octal
  300.          4_294_967_296       # underline for legibility
  301.  
  302.      String literals are usually delimited by either single or double quotes.
  303.      They work much like shell quotes: double-quoted string literals are
  304.      subject to backslash and variable substitution; single-quoted strings are
  305.      not (except for "\'" and "\\").  The usual Unix backslash rules apply for
  306.      making characters such as newline, tab, etc., as well as some more exotic
  307.      forms.  See the section on _Q_u_o_t_e _a_n_d _Q_u_o_t_e_l_i_k_e _O_p_e_r_a_t_o_r_s in the _p_e_r_l_o_p
  308.      manpage for a list.
  309.  
  310.      Octal or hex representations in string literals (e.g. '0xffff') are not
  311.      automatically converted to their integer representation.  The _h_e_x() and
  312.      _o_c_t() functions make these conversions for you.  See the hex entry in the
  313.      _p_e_r_l_f_u_n_c manpage and the oct entry in the _p_e_r_l_f_u_n_c manpage for more
  314.      details.
  315.  
  316.      You can also embed newlines directly in your strings, i.e., they can end
  317.      on a different line than they begin.  This is nice, but if you forget
  318.      your trailing quote, the error will not be reported until Perl finds
  319.      another line containing the quote character, which may be much further on
  320.      in the script.  Variable substitution inside strings is limited to scalar
  321.      variables, arrays, and array slices.  (In other words, names beginning
  322.      with $ or @, followed by an optional bracketed expression as a
  323.      subscript.)  The following code segment prints out "The price is $100."
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  335.  
  336.  
  337.  
  338.          $Price = '$100';    # not interpreted
  339.          print "The price is $Price.\n";     # interpreted
  340.  
  341.      As in some shells, you can put curly brackets around the name to delimit
  342.      it from following alphanumerics.  In fact, an identifier within such
  343.      curlies is forced to be a string, as is any single identifier within a
  344.      hash subscript.  Our earlier example,
  345.  
  346.          $days{'Feb'}
  347.  
  348.      can be written as
  349.  
  350.          $days{Feb}
  351.  
  352.      and the quotes will be assumed automatically.  But anything more
  353.      complicated in the subscript will be interpreted as an expression.
  354.  
  355.      Note that a single-quoted string must be separated from a preceding word
  356.      by a space, because single quote is a valid (though deprecated) character
  357.      in a variable name (see the Packages entry in the _p_e_r_l_m_o_d manpage).
  358.  
  359.      Three special literals are __FILE__, __LINE__, and __PACKAGE__, which
  360.      represent the current filename, line number, and package name at that
  361.      point in your program.  They may be used only as separate tokens; they
  362.      will not be interpolated into strings.  If there is no current package
  363.      (due to a package; directive), __PACKAGE__ is the undefined value.
  364.  
  365.      The tokens __END__ and __DATA__ may be used to indicate the logical end
  366.      of the script before the actual end of file.  Any following text is
  367.      ignored, but may be read via a DATA filehandle: main::DATA for __END__,
  368.      or PACKNAME::DATA (where PACKNAME is the current package) for __DATA__.
  369.      The two control characters ^D and ^Z are synonyms for __END__ (or
  370.      __DATA__ in a module).  See the _S_e_l_f_L_o_a_d_e_r manpage for more description
  371.      of __DATA__, and an example of its use.  Note that you cannot read from
  372.      the DATA filehandle in a BEGIN block: the BEGIN block is executed as soon
  373.      as it is seen (during compilation), at which point the corresponding
  374.      __DATA__ (or __END__) token has not yet been seen.
  375.  
  376.      A word that has no other interpretation in the grammar will be treated as
  377.      if it were a quoted string.  These are known as "barewords".  As with
  378.      filehandles and labels, a bareword that consists entirely of lowercase
  379.      letters risks conflict with future reserved words, and if you use the ----wwww
  380.      switch, Perl will warn you about any such words.  Some people may wish to
  381.      outlaw barewords entirely.  If you say
  382.  
  383.          use strict 'subs';
  384.  
  385.      then any bareword that would NOT be interpreted as a subroutine call
  386.      produces a compile-time error instead.  The restriction lasts to the end
  387.      of the enclosing block.  An inner block may countermand this by saying no
  388.      strict 'subs'.
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  401.  
  402.  
  403.  
  404.      Array variables are interpolated into double-quoted strings by joining
  405.      all the elements of the array with the delimiter specified in the $"
  406.      variable ($LIST_SEPARATOR in English), space by default.  The following
  407.      are equivalent:
  408.  
  409.          $temp = join($",@ARGV);
  410.          system "echo $temp";
  411.  
  412.          system "echo @ARGV";
  413.  
  414.      Within search patterns (which also undergo double-quotish substitution)
  415.      there is a bad ambiguity:  Is /$foo[bar]/ to be interpreted as
  416.      /${foo}[bar]/ (where [bar] is a character class for the regular
  417.      expression) or as /${foo[bar]}/ (where [bar] is the subscript to array
  418.      @foo)?  If @foo doesn't otherwise exist, then it's obviously a character
  419.      class.  If @foo exists, Perl takes a good guess about [bar], and is
  420.      almost always right.  If it does guess wrong, or if you're just plain
  421.      paranoid, you can force the correct interpretation with curly brackets as
  422.      above.
  423.  
  424.      A line-oriented form of quoting is based on the shell "here-doc" syntax.
  425.      Following a << you specify a string to terminate the quoted material, and
  426.      all lines following the current line down to the terminating string are
  427.      the value of the item.  The terminating string may be either an
  428.      identifier (a word), or some quoted text.  If quoted, the type of quotes
  429.      you use determines the treatment of the text, just as in regular quoting.
  430.      An unquoted identifier works like double quotes.  There must be no space
  431.      between the << and the identifier.  (If you put a space it will be
  432.      treated as a null identifier, which is valid, and matches the first empty
  433.      line.)  The terminating string must appear by itself (unquoted and with
  434.      no surrounding whitespace) on the terminating line.
  435.  
  436.              print <<EOF;
  437.          The price is $Price.
  438.          EOF
  439.  
  440.              print <<"EOF";  # same as above
  441.          The price is $Price.
  442.          EOF
  443.  
  444.              print <<`EOC`;  # execute commands
  445.          echo hi there
  446.          echo lo there
  447.          EOC
  448.  
  449.              print <<"foo", <<"bar"; # you can stack them
  450.          I said foo.
  451.          foo
  452.          I said bar.
  453.          bar
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  467.  
  468.  
  469.  
  470.              myfunc(<<"THIS", 23, <<'THAT');
  471.          Here's a line
  472.          or two.
  473.          THIS
  474.          and here's another.
  475.          THAT
  476.  
  477.      Just don't forget that you have to put a semicolon on the end to finish
  478.      the statement, as Perl doesn't know you're not going to try to do this:
  479.  
  480.              print <<ABC
  481.          179231
  482.          ABC
  483.              + 20;
  484.  
  485.  
  486.      LLLLiiiisssstttt vvvvaaaalllluuuueeee ccccoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  487.  
  488.      List values are denoted by separating individual values by commas (and
  489.      enclosing the list in parentheses where precedence requires it):
  490.  
  491.          (LIST)
  492.  
  493.      In a context not requiring a list value, the value of the list literal is
  494.      the value of the final element, as with the C comma operator.  For
  495.      example,
  496.  
  497.          @foo = ('cc', '-E', $bar);
  498.  
  499.      assigns the entire list value to array foo, but
  500.  
  501.          $foo = ('cc', '-E', $bar);
  502.  
  503.      assigns the value of variable bar to variable foo.  Note that the value
  504.      of an actual array in a scalar context is the length of the array; the
  505.      following assigns the value 3 to $foo:
  506.  
  507.          @foo = ('cc', '-E', $bar);
  508.          $foo = @foo;                # $foo gets 3
  509.  
  510.      You may have an optional comma before the closing parenthesis of a list
  511.      literal, so that you can say:
  512.  
  513.          @foo = (
  514.              1,
  515.              2,
  516.              3,
  517.          );
  518.  
  519.      LISTs do automatic interpolation of sublists.  That is, when a LIST is
  520.      evaluated, each element of the list is evaluated in a list context, and
  521.      the resulting list value is interpolated into LIST just as if each
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  533.  
  534.  
  535.  
  536.      individual element were a member of LIST.  Thus arrays lose their
  537.      identity in a LIST--the list
  538.  
  539.          (@foo,@bar,&SomeSub)
  540.  
  541.      contains all the elements of @foo followed by all the elements of @bar,
  542.      followed by all the elements returned by the subroutine named SomeSub
  543.      when it's called in a list context.  To make a list reference that does
  544.      _N_O_T interpolate, see the _p_e_r_l_r_e_f manpage.
  545.  
  546.      The null list is represented by ().  Interpolating it in a list has no
  547.      effect.  Thus ((),(),()) is equivalent to ().  Similarly, interpolating
  548.      an array with no elements is the same as if no array had been
  549.      interpolated at that point.
  550.  
  551.      A list value may also be subscripted like a normal array.  You must put
  552.      the list in parentheses to avoid ambiguity.  For example:
  553.  
  554.          # Stat returns list value.
  555.          $time = (stat($file))[8];
  556.  
  557.          # SYNTAX ERROR HERE.
  558.          $time = stat($file)[8];  # OOPS, FORGOT PARENTHESES
  559.  
  560.          # Find a hex digit.
  561.          $hexdigit = ('a','b','c','d','e','f')[$digit-10];
  562.  
  563.          # A "reverse comma operator".
  564.          return (pop(@foo),pop(@foo))[0];
  565.  
  566.      You may assign to undef in a list.  This is useful for throwing away some
  567.      of the return values of a function:
  568.  
  569.          ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
  570.  
  571.      Lists may be assigned to if and only if each element of the list is legal
  572.      to assign to:
  573.  
  574.          ($a, $b, $c) = (1, 2, 3);
  575.  
  576.          ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00);
  577.  
  578.      Array assignment in a scalar context returns the number of elements
  579.      produced by the expression on the right side of the assignment:
  580.  
  581.          $x = (($foo,$bar) = (3,2,1));       # set $x to 3, not 2
  582.          $x = (($foo,$bar) = f());           # set $x to f()'s return count
  583.  
  584.      This is very handy when you want to do a list assignment in a Boolean
  585.      context, because most list functions return a null list when finished,
  586.      which when assigned produces a 0, which is interpreted as FALSE.
  587.  
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  599.  
  600.  
  601.  
  602.      The final element may be an array or a hash:
  603.  
  604.          ($a, $b, @rest) = split;
  605.          local($a, $b, %rest) = @_;
  606.  
  607.      You can actually put an array or hash anywhere in the list, but the first
  608.      one in the list will soak up all the values, and anything after it will
  609.      get a null value.  This may be useful in a _l_o_c_a_l() or _m_y().
  610.  
  611.      A hash literal contains pairs of values to be interpreted as a key and a
  612.      value:
  613.  
  614.          # same as map assignment above
  615.          %map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
  616.  
  617.      While literal lists and named arrays are usually interchangeable, that's
  618.      not the case for hashes.  Just because you can subscript a list value
  619.      like a normal array does not mean that you can subscript a list value as
  620.      a hash.  Likewise, hashes included as parts of other lists (including
  621.      parameters lists and return lists from functions) always flatten out into
  622.      key/value pairs.  That's why it's good to use references sometimes.
  623.  
  624.      It is often more readable to use the => operator between key/value pairs.
  625.      The => operator is mostly just a more visually distinctive synonym for a
  626.      comma, but it also arranges for its left-hand operand to be interpreted
  627.      as a string, if it's a bareword which would be a legal identifier.  This
  628.      makes it nice for initializing hashes:
  629.  
  630.          %map = (
  631.                       red   => 0x00f,
  632.                       blue  => 0x0f0,
  633.                       green => 0xf00,
  634.         );
  635.  
  636.      or for initializing hash references to be used as records:
  637.  
  638.          $rec = {
  639.                      witch => 'Mable the Merciless',
  640.                      cat   => 'Fluffy the Ferocious',
  641.                      date  => '10/31/1776',
  642.          };
  643.  
  644.      or for using call-by-named-parameter to complicated functions:
  645.  
  646.         $field = $query->radio_group(
  647.                     name      => 'group_name',
  648.                     values    => ['eenie','meenie','minie'],
  649.                     default   => 'meenie',
  650.                     linebreak => 'true',
  651.                     labels    => \%labels
  652.         );
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))                                                        PPPPEEEERRRRLLLLDDDDAAAATTTTAAAA((((1111))))
  665.  
  666.  
  667.  
  668.      Note that just because a hash is initialized in that order doesn't mean
  669.      that it comes out in that order.  See the sort entry in the _p_e_r_l_f_u_n_c
  670.      manpage for examples of how to arrange for an output ordering.
  671.  
  672.      TTTTyyyyppppeeeegggglllloooobbbbssss aaaannnndddd FFFFiiiilllleeeehhhhaaaannnnddddlllleeeessss
  673.  
  674.      Perl uses an internal type called a _t_y_p_e_g_l_o_b to hold an entire symbol
  675.      table entry.  The type prefix of a typeglob is a *, because it represents
  676.      all types.  This used to be the preferred way to pass arrays and hashes
  677.      by reference into a function, but now that we have real references, this
  678.      is seldom needed.  It also used to be the preferred way to pass
  679.      filehandles into a function, but now that we have the *foo{THING}
  680.      notation it isn't often needed for that, either.  It is still needed to
  681.      pass new filehandles into functions (*HANDLE{IO} only works if HANDLE has
  682.      already been used).
  683.  
  684.      If you need to use a typeglob to save away a filehandle, do it this way:
  685.  
  686.          $fh = *STDOUT;
  687.  
  688.      or perhaps as a real reference, like this:
  689.  
  690.          $fh = \*STDOUT;
  691.  
  692.      This is also a way to create a local filehandle.  For example:
  693.  
  694.          sub newopen {
  695.              my $path = shift;
  696.              local *FH;  # not my!
  697.              open (FH, $path) || return undef;
  698.              return *FH;
  699.          }
  700.          $fh = newopen('/etc/passwd');
  701.  
  702.      Another way to create local filehandles is with IO::Handle and its ilk,
  703.      see the bottom of the open() entry in the _p_e_r_l_f_u_n_c manpage.
  704.  
  705.      See the _p_e_r_l_r_e_f manpage, the _p_e_r_l_s_u_b manpage, and the section on _S_y_m_b_o_l
  706.      _T_a_b_l_e_s in the _p_e_r_l_m_o_d manpage for more discussion on typeglobs.
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.